ActionScreen = function(ctx){

	this.ctx = ctx;
	this.images = preloader.images;
	this.type='ActionScreen';
	this.map = new Map(ctx);
	this.player = new Player(ctx);
	this.persons = [];
	this.layout = null;
	this.handlerIsWorking = false;
	this.gameloop = null;
	this.stories = [];


	this.init = function(gameloop){

		var pm = {},
			px = 0,
			py = 0
			pi = '',
			person = null,
			sc = 0,
			stories = [],
			rnd = 0
			pcnt = 0,
			cnt = 0,
			ol = 0,
			stories = [];

		this.gameloop = gameloop;

		for( sc in pStories ){
			if( pStories.hasOwnProperty(sc) ){
				stories.push(
					new Story(pStories[sc],sc)
				);
			}
		}

		this.stories = new Stories(stories);
		
		for( var i in pPersons ) {

			if( pPersons.hasOwnProperty(i) ) {

				px = Math.random()*3;
				py = Math.random()*4;
				pi = px+' '+py;
				var d = 0;
				do{
					px = parseInt(Math.random()*3);
					py = parseInt(Math.random()*4);
					pi = px+' '+py;
					d+=1;
				}while( typeof pm[pi] != 'undefined');
				
				pm[pi] = true;
				person = new Person(
					this.ctx,
					'person_' + i + '.png',
					i,
					pPersons[i].fullname,
					py,
					px,
					this.player,
					this.gameloop
				);

				person.init(this.stories);
				

				this.persons.push(person);
			}


		}

		this.stories.init(this.persons);

		$(document).keydown(function(event){

			if( event.keyCode == 32 ){
				event.preventDefault();
				if( mainLoop.activeWindow !== null ) {
					mainLoop.activeWindow.fadeOut('fast');
					$('#talksBackground').fadeOut();
					mainLoop.activeWindow = null;
				}
			}

			if( this.handlerIsWorking || mainLoop.enabled == false ) {
				return;
			}

			this.handlerIsWorking = true;

			var x = this.player.position.x,
				y = this.player.position.y,
				layout = this.layout;

			switch(event.keyCode){
				case 119:
				case 87:
					if( layout[0][1] !== null ) {
						this.player.position.y -= 1;
					}
					event.preventDefault();
					break;
				case 119:
				case 68:
					if( layout[1][2] !== null ) {
						this.player.position.x += 1;
					}
					event.preventDefault();
					break;
				case 115:
				case 83:
					if( layout[2][1] !== null ) {
						this.player.position.y += 1;
					}
					event.preventDefault();
					break;
				case 97:
				case 65:
					if( layout[1][0] !== null ) {
						this.player.position.x -= 1;
					}
					event.preventDefault();
					break;
			}
			this.handlerIsWorking = false;
			
		}.bind(this));

	};

	this.loop = function(){

		var p = 0;

		this.ctx.fillStyle = '#000000';
		this.ctx.fillRect(0,0,700,700);

		this.layout = this.map.getLayout(this.player.position.y,this.player.position.x,this.persons);
		
		for( p = this.persons.length;p;p--){
			this.layout = this.persons[p-1].loop(this.layout);			
		}

		this.map.draw(this.layout,this.player);


		return null;

	};

}